#!/usr/bin/python

# ====================
# BitDefender.py
# ====================


from sharedDefinitions import *
import sys
import tempfile
import datetime
import logging


INPUT_DIR = '/home/ubuntu/virus-data/raw-data'
INPUT_FILE = ''
OUTPUT_DIR = '/home/ubuntu/virus-data/processed-data'

masterPlatformList = []
masterRiskList = []

def Print2File(printStr, fileName):
    tfile = open(fileName, 'a')
    tfile.write(printStr)
    tfile.write ('\n')
    tfile.close()
    

def printBlockAsXML(cleanBlock):

    fd,tempfilename = tempfile.mkstemp(suffix='.xml', prefix='Bitdefender_', dir=OUTPUT_DIR, text=True)    
    #print 'writing virus data to: ' + tempfilename   
    Print2File('<?xml version="1.0" encoding="UTF-8"?>',tempfilename)
    Print2File('<virusModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="VirusModel.xsd">',tempfilename)
     


    # =========================
    # Get Name
    # =========================
    virusName = getSubStrFromList('<h1>', '</h1>', cleanBlock)
    Print2File( '  <name>' + stripHTML(virusName.strip()) + '</name>',tempfilename)
    print 'found virus name: ' + stripHTML(virusName.strip())

    # ========================
    # clean up the block
    # ========================
    cleanBlock = stripHTMLfromList(cleanBlock)
    cleanBlock = stripEmptyLines(cleanBlock)

    # =========================
    # aliases
    # =========================
    alias = getSubStrFromList(' ( ', ' )', cleanBlock)
    Print2File( '  <aliases>',tempfilename)
    if alias != None:
        aliases = alias.split(',')
        for a in aliases:
            Print2File( '    <alias>' + a + '</alias>',tempfilename)
    else:
        Print2File( '    <alias>No aliases found</alias>',tempfilename)
    Print2File( '  </aliases>',tempfilename)



    # =========================
    # Get DiscoveryDate
    # =========================
    discoverDate = getNextLine('Discovered:', cleanBlock)
    if discoverDate != None:
        print 'discoverDate: ' +  stripHTML(discoverDate.strip())
        strDate = stripHTML(discoverDate.strip())
        fmtDateIn = '%Y %b %d'
        fmtDateOut = '%Y-%m-%dT%H:%M:%SZ'
        formattedDate = datetime.datetime.strptime(strDate, fmtDateIn)         
        Print2File( '  <discoveryDate>' + formattedDate.strftime(fmtDateOut) + '</discoveryDate>',tempfilename)


    # =========================
    # Get LengthInBytes 
    # =========================
    length = getBlockReturnString('Size:', 'Discovered:',  cleanBlock).strip()
    intLength = convertLengthStringToInt(length)
    if intLength != None:
        Print2File( '  <lengthInBytes>' + intLength + '</lengthInBytes>',tempfilename)
    else:
        Print2File( '  <lengthInBytes>-1</lengthInBytes>',tempfilename)

    # =========================
    # InformationSource 
    # =========================    
    Print2File( '  <informationsource>www.bitdefender.com</informationsource>',tempfilename)
   

    # =========================
    # detectionMethod
    # =========================
    Print2File( '  <detectionMethod>BitDefender software</detectionMethod>',tempfilename)


    # =========================
    # origin
    # =========================
    Print2File( '  <origin>Origin not available for this virus</origin>',tempfilename)

    # =========================
    # VirusTypes
    # =========================
    Print2File( '  <virustypes>',tempfilename)
    Print2File( '    <type>virus</type>',tempfilename)
    Print2File( '  </virustypes>',tempfilename)
    

    # =========================
    # removalInstructions " <p>Removal instructions:</p>"
    # =========================
    removal = getBlockReturnString('Removal instructions:', 'ANALYZED BY:', cleanBlock).strip()    
    if len(stripHTML(removal).strip()) != 0:
        Print2File( '  <removalInstructions>' + stripHTML(removal) + '\n  </removalInstructions>',tempfilename)
    else:
        Print2File( '  <removalInstructions>Removal instructions not available for this virus</removalInstructions>',tempfilename)


    # =========================
    # technicalDetails
    # =========================
    techDetails = getBlockReturnString('TECHNICAL DESCRIPTION:', 'Removal instructions:', cleanBlock).strip()
    if len(stripHTML(techDetails).strip()) != 0:
        Print2File( '  <technicalDetails>' + stripHTML(techDetails) + '\n  </technicalDetails>',tempfilename)
        #print 'Technical Details: ' + stripHTML(techDetails)
    else:
        Print2File( '  <technicalDetails>No technical details available for this virus*</technicalDetails>',tempfilename)


    # =========================
    # Get Platform
    # =========================
    Print2File( '  <platforms>',tempfilename)
    Print2File( '    <platform>Windows</platform>',tempfilename)
    Print2File( '  </platforms>',tempfilename)

    # =========================
    # Get DamageRisk 
    # =========================
    risk = getBlockReturnString('Damage:', 'Size:', cleanBlock).strip()
    if risk != None:
        Print2File( '  <damageRisk>' + stripHTML(risk.strip()).strip() + '</damageRisk>',tempfilename)


    # =========================
    # Get Symptoms
    # =========================    
    symptoms = getBlockReturnString('SYMPTOMS:', 'TECHNICAL DESCRIPTION:', cleanBlock).strip()  
    #print 'len(stripHTML(symptoms)) = ' + len(stripHTML(symptoms))
    if len(stripHTML(symptoms).strip()) != 0:    
        Print2File( '  <symtoms>' + stripHTML(symptoms) + '\n  </symtoms>',tempfilename)
        #print 'Symptoms: ' + stripHTML(symptoms)    
    else:
        Print2File( '  <symtoms>No symptoms available for this virus</symtoms>',tempfilename)

    # =========================
    # Get GeneralComments
    # =========================
    Print2File( '  <generalComments>No comments available for this virus</generalComments>',tempfilename)

    # ==========================
    # end of the <Virus> block
    # ==========================
    Print2File( '</virusModel>\n\n',tempfilename)









def printReferenceDataAsXML():
    
    # =========================
    # Platforms
    # =========================           

    if masterPlatformList != []:
        platformSet = set(masterPlatformList)
        fd,tempfilename = tempfile.mkstemp(suffix='.xml', prefix='Platforms_', dir=OUTPUT_DIR, text=True)    
        #print 'writing Platform reference data to: ' + tempfilename
        Print2File( '<?xml version="1.0" encoding="UTF-8"?>',tempfilename)
        Print2File( '<platformsModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PlatformsModel.xsd">',tempfilename)
        Print2File( '  <platforms>',tempfilename)
        for s in platformSet:            
            Print2File( '    <name>' + s + '</name>',tempfilename)
            
        Print2File( '  </platforms>',tempfilename)
        Print2File( '</platformsModel>',tempfilename)    
    
    # =========================
    # VirusTypes
    # =========================
    fd,tempfilename = tempfile.mkstemp(suffix='.xml', prefix='VirusTypes_', dir=OUTPUT_DIR, text=True)
    #print 'writing Platform reference data to: ' + tempfilename
    Print2File( '<?xml version="1.0" encoding="UTF-8"?>',tempfilename)
    Print2File( '<virustypesModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PlatformsModel.xsd">',tempfilename)
    Print2File( '  <virustypes>',tempfilename)
    Print2File( '    <virus>virus</virus>',tempfilename)
    Print2File( '  </virustypes>',tempfilename)
    Print2File( '</virustypesModel>',tempfilename)



def captureReferenceData(cleanBlock):

    # =========================
    # Platforms
    # =========================           
    platformStr = getNextLine('<dt>Systems Affected: </dt>', cleanBlock)
    if platformStr != None:        
        platformList = platformStr.split(',')
        if platformList != None:
            for s in platformList:
                masterPlatformList.append(stripHTML(s.strip()))

        
def processFile(fileName):
    INPUT_FILE = fileName
    print 'INPUT_FILE = ' + fileName    
    try:
        f  = open(INPUT_DIR + '/' + INPUT_FILE, 'r')
    except IOError as e:
        print fileName + ' cannot be opened as a file'
        return

    xml = f.read()
    f.close()
    
    xmlLines = xml.splitlines()
    clean = stripEmptyLines(xmlLines)
    
    # ==============================
    # write InformationSource file
    # ==============================
    fd,tempfilename = tempfile.mkstemp(suffix='.xml', prefix='InformationSource_', dir=OUTPUT_DIR, text=True)    
    #print 'writing InformationSource reference data to: ' + tempfilename  
    
    Print2File( '<?xml version="1.0" encoding="UTF-8"?>',tempfilename)
    Print2File( '<informationsourceModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="InformationsourceModel.xsd">',tempfilename)
    
    Print2File( '  <name>www.bitdefender.com</name>',tempfilename)
    Print2File( '  <IP_Address>66.132.220.42</IP_Address>',tempfilename)
    
    Print2File( '</informationsourceModel>',tempfilename)
    
    
    
    # =============================
    # create the virus data files
    # =============================
    # <h4><q><q><q>BITDEFENDER RESOURCE CENTER</q></q></q></h4>
    while True:    
        currBlock = getBlock('BITDEFENDER RESOURCE CENTER', 'ANALYZED BY:', clean)
        if currBlock != []:
            printBlockAsXML(currBlock)    
    
        clean = stripBlock('BITDEFENDER RESOURCE CENTER', 'ANALYZED BY:', clean)
        if clean == []:
            break


    # =============================
    # create the reference files
    # =============================
    clean = stripEmptyLines(xmlLines)
    while True:    
        currBlock = getBlock('<li>Virus Information for -', ' <p>ANALYZED BY:</p>', clean)
        if currBlock != []:
            captureReferenceData(currBlock)    
    
        clean = stripBlock('<li>Virus Information for -', ' <p>ANALYZED BY:</p>', clean)
        if clean == []:
            break
    
                       
    printReferenceDataAsXML()
    

                         
    













